/***********************************************************
  Copyright (C), 2015, Water's Space
  File name:    lib_math.c
  Author:       Water (sszhouplus@qq.com)
  Version:      0.1
  Date:         2015-02-02
  Description:  Math Lib Header File for Audio Used
  OpenSource:   https://git.oschina.net/webwater/AudioKits.git
  History:
    1. Date:    2015-02-02
       Author:  Water (sszhouplus@qq.com)
       Modification: Create
***********************************************************/
#include "lib_math.h"


#include "tab_sine.tab"     // table for lib_sin/lib_sin_lite function used

/**
  * Function:   lib_sin
  * @brief:     audio used sin lib function, fixed point
  * @param:
  *             phase: fixed point, 2*pi = 2^22 = 0x400000
  *                    ex. pi/4 = 0x80000
  * @return:
  *             return sin(phase) fixed point value Q1.31
  * Others:
  */
long lib_sin(long phase)
{
    int sflag=0;
    const long *pstab;
    long sa, sb, da, db;

    if (phase < 0)
    {
        phase = -phase;
        sflag = -1;
    }

    da = phase & 0xFFF;
    db = 0x1000 - da;
    phase >>= 12;
    pstab = tab_sine_1024_32bits_Q19 + phase;
    sa = *pstab++;
    sb = *pstab;

    if (sflag < 0)
    {
        return -(sa*db + sb*da);
    }

    return (sa*db + sb*da);
}


/**
  * Function:   lib_sin_lite
  * @brief:     audio used sin lib function lite version, fixed point
  * @param:
  *             phase: fixed point, 2*pi = 2^10 = 0x400
  *                    ex. pi/4 = 0x80
  * @return:
  *             return sin(phase) fixed point value Q1.31
  * Others:
  */
long lib_sin_lite(unsigned long phase)
{
    return tab_sine_1024_32bits_Q31[phase & 0x3FF];
}






